<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE xsl:stylesheet SYSTEM "lang://blogs">

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">	
	
	<xsl:include href="../../main.xsl" />
	<xsl:include href="../menu.xsl" />
	<xsl:include href="../../common/form.xsl" />
	<xsl:include href="blocks.xsl" />
	
	<!-- Запускаем рендер страницы -->
	<xsl:template match="/data">
		<html>
		
		<head>
			<title>
				<xsl:value-of select="$pageTitle" />
			</title>
			<xsl:call-template name="meta-head" />
			<link rel="stylesheet" href="{$baseURI}/css/blogs.css" type="text/css" media="all" />
			<link rel="stylesheet" href="{$baseURI}/css/forms.css" type="text/css" media="all" />
			<script type="text/javascript" src="{$baseURI}/js/blogs.js" />
			<script type="text/javascript" src="{$baseURI}/js/simpleEditor.js" />
			
			<xsl:comment>[if IE]&gt;&lt;link rel="stylesheet" href="<xsl:value-of select="$baseURI"/>/css/blogs-ie.css" type="text/css" media="all" /&gt;&lt;![endif]</xsl:comment>
		</head>
		<body>
			
			<xsl:call-template name="top-userbar" />
			
			<!-- Header -->
			<div id="header">
				<xsl:call-template name="logo" />
				
				<!-- Menu -->
				<xsl:call-template name="menu" />
			</div>
			<!-- /Header -->
			
			<!-- Blocks -->
			<div id="blocks">
				<div id="blocks_inner">
					<xsl:call-template name="default-blocks-top" />
					
					<xsl:call-template name="post_add_form_blocks" />
				</div>
			</div>
			<!-- // Blocks -->
			
			<!-- Content -->
			<div id="content">
				<div id="content_inner">
					<xsl:apply-templates select="response" />
				</div>
			</div>
			<!-- // Content -->
			
			<!-- Address -->
			<xsl:call-template name="footer" />
		</body>
		</html>
	</xsl:template>
	
	<xsl:template match="response">
		<xsl:choose>
			<xsl:when test="@type = 'article'">
				<xsl:call-template name="add_article" />
			</xsl:when>
			<xsl:when test="@type = 'link'">
				<xsl:call-template name="add_link" />
			</xsl:when>
		</xsl:choose>
	</xsl:template>
	
	<xsl:template name="add_article">
		<h1>
			<xsl:if test="$method = 'add'">
				<xsl:text>&labelNewArticle;</xsl:text>
			</xsl:if>
			<xsl:if test="$method = 'edit'">
				<xsl:text>&labelEditArticle;</xsl:text>
			</xsl:if>
		</h1>
		<form action="{$baseURI}/blogs/add/article/save" method="post">
			<xsl:if test="$method = 'edit'">
				<xsl:attribute name="action">
					<xsl:value-of select="concat ($baseURI, '/blogs/edit/article/', $formPrefix, '/save')" />
				</xsl:attribute>
			</xsl:if>
			
			<xsl:for-each select="form/field">
				<xsl:apply-templates select="." />
			</xsl:for-each>
			<p>
				<input type="submit" value="&labelToPublic;" />		
			</p>
		</form>
	</xsl:template>
	
	<xsl:template name="add_link">
		<h1>
			<xsl:if test="$method = 'add'">
				<xsl:text>&labelNewLink;</xsl:text>
			</xsl:if>
			<xsl:if test="$method = 'edit'">
				<xsl:text>&labelEditLink;</xsl:text>
			</xsl:if>
		</h1>
		<form action="{$baseURI}/blogs/add/link/save" method="post">
			<xsl:if test="$method = 'edit'">
				<xsl:attribute name="action">
					<xsl:value-of select="concat ($baseURI, '/blogs/edit/link/', $formPrefix, '/save')" />
				</xsl:attribute>
			</xsl:if>
			
			<input id="referer" type="hidden" name="referer" value="" />
			<script type="text/javascript">document.getElementById('referer').value=document.location.href;</script>
			
			<xsl:for-each select="form/field">
				<xsl:apply-templates select="." />
			</xsl:for-each>
			<p>
				<input type="submit" value="&labelToPublic;" />		
			</p>
		</form>
	</xsl:template>
	
	<xsl:template match="form/field[@character='integer' and @name='blog_id']">
		<div class="form_field">
			<div class="form_field_label">
				<label for="blog_select">
					<xsl:text>&labelToWhatBlog;:</xsl:text>
				</label>
			</div>
			<div class="form_field_input">
				<select id="blog_select" onchange="showBlogDescription (this.value);" class="form_input_guide">
					<xsl:attribute name="name">
						<xsl:value-of select="concat ('data[', $formPrefix, '][', @name, ']')" />
					</xsl:attribute>
					<option value="0">&labelPersonalBlog;</option>
					
					<xsl:apply-templates select="document(concat('xmodule://blogs/getUserBlogsSelection/', value))/data/response/object" mode="blog-option">
						<xsl:sort select="property[@name='blog_title']/value" />
					</xsl:apply-templates>
				
				</select>
				<xsl:if test="error != ''">
					<br /><span class="form_field_error"><xsl:value-of select="error" /></span>
				</xsl:if>
				<xsl:if test="help != ''">
					<br /><span class="form_field_help"><xsl:value-of select="help" /></span>
				</xsl:if>
			</div>
		</div>
		<script type="text/javascript">
			showBlogDescription ($('#blog_select').val());
		</script>
	</xsl:template>
	
	<!-- BLOG OPTION -->
	<xsl:template match="object[@type='blog_member']" mode="blog-option">
		<option>
			<xsl:if test="@selected = '1'">
				<xsl:attribute name="selected"><xsl:text>selected</xsl:text></xsl:attribute>
			</xsl:if>
			<xsl:attribute name="value">
				<xsl:value-of select="property[@name='blog_id']/value" />
			</xsl:attribute>
			<xsl:value-of select="property[@name='blog_title']/value" />
			<xsl:if test="@closed = '1'">
				<xsl:text> (закрыт для новых публикаций)</xsl:text>
			</xsl:if>
		</option>
	</xsl:template>
	
</xsl:stylesheet>